#include "stdafx.h"
#include "UDPWriter.h"


UDPWriter::UDPWriter(SOCKET socket): m_socket(socket)
{
}

UDPWriter::UDPWriter(const char *addr, int port)
{
}

UDPWriter::~UDPWriter()
{
	close();
}

void UDPWriter::close()
{
	if(m_socket != INVALID_SOCKET)
	{
		closesocket(m_socket);
		m_socket = INVALID_SOCKET;
	}
}

int UDPWriter::write(const char* buf, int len, const sockaddr_in* to, int tolen)
{
	if(m_socket == INVALID_SOCKET)
		return -1;
	if(len <= 0)
		return -1;
	int ret = sendto(m_socket, buf, len, 0, (sockaddr*)to, tolen);
	if(ret == INVALID_SOCKET)
		return -1;
	if(ret < len)
	{
		
	}
	return ret;
}

int UDPWriter::write(const char* buf, int len, const char* ip, int port)
{
	sockaddr_in to;
	int tolen =sizeof(to);
	to.sin_family=AF_INET;
	to.sin_port=htons(port);
	to.sin_addr.s_addr=inet_addr(ip); 
	return write(buf, len, &to, tolen);
}